﻿using Common;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace WebAppBase
{
    public partial class ParkingSetting: IParkingSetting
    {
        public int HistDateBegin { get; set; } = 3; //minutes = 9
        public long IntervalSeconds { get; set; } = 20;
        public long AllowanceSeconds { get; set; } = 181;
        public int MaxIntervalTimes { get; set; } = 10;
        public int IntervalTimes { get; set; } = 10;
        public int IntervalMinutes { get; set; } = 11; //For job task
        public bool OnlyRegistered { get; set; } = true; 
        public int MinTimesValid { get; set; } = 6; 
        public int HistoryRecordRemain { get; set; } = 7;   //How long  remain ? //  history days 

        public static ParkingSetting Get()
        {
            string jsonFileName = "ParkingSetting.Json";
            string baseDirectoryPath = System.IO.Path.GetFullPath("./");//AppDomain.CurrentDomain.BaseDirectory;
            string pathFileName = Path.Combine(baseDirectoryPath, jsonFileName);
            if(File.Exists(pathFileName))
            {
                string json = CommonBase.ReadConfigJson(pathFileName);
                ParkingSetting parkingSetting = JsonConvert.DeserializeObject<ParkingSetting>(json);
                return parkingSetting;
            }
            else
            {
                return new ParkingSetting();
            }
        }
        public static bool Save(ParkingSetting setting)
        {
            string jsonFileName = "ParkingSetting.Json";
            string baseDirectoryPath = AppDomain.CurrentDomain.BaseDirectory;
            string pathFileName = Path.Combine(baseDirectoryPath, jsonFileName);
             
            if(File.Exists(pathFileName))
            {
                File.Delete(pathFileName);
            }
            string json = JsonConvert.SerializeObject(setting);

            bool result = CommonBase.SaveDataJson(json, pathFileName);

            return result;
        }
    }

    public interface  IParkingSetting
    {
        public int HistDateBegin { get; set; }
        public long IntervalSeconds { get; set; }
        public long AllowanceSeconds { get; set; }
        public int MaxIntervalTimes { get; set; }
        public int IntervalTimes { get; set; }
        public int IntervalMinutes { get; set; }
        public bool OnlyRegistered { get; set; } 
    }
}
